/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.gaib.dmc;

import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import weka.classifiers.Classifier;
import weka.core.FastVector;
import weka.core.Attribute;
import weka.core.Instance;
import weka.core.Instances;

/**
 *
 * @author Fauzan
 */
public class GaIBReviewer implements Reviewer{

    final String modelPath = "model1.model";
    Classifier cls;
    Instances dataInstances;
    List<Instance> dataSet;
    
    //Atributes
    FastVector atts;
    
    public GaIBReviewer() {
        cls = LoadModel(modelPath);
        
        SetUpAttribures(); 
    }

    private void SetUpAttribures() {
        atts = new FastVector();

        String[] atrributeNames = new String[] {"sessionNo", "startHour",
            "startWeekday", "duration", "cCount", "cMinPrice", "cMaxPrice",
            "cSumPrice", "bCount", "bMinPrice", "bMaxPrice", "bSumPrice",
            "bStep", "onlineStatus" , "availability" , "customerID" , "maxVal",
            "customerScore","accountLifetime","payments","age","address",
            "lastOrder","order"
        };

        // - sessionNo,startHour,startWeekday,duration,cCount,
        // - cMinPrice,cMaxPrice,cSumPrice,bCount,bMinPrice,bMaxPrice,bSumPrice,bStep (Numeric)
        for(int i=0; i < 13 ; i++) {
            atts.addElement(new Attribute(atrributeNames[i]));
        }
        //Instance ins = new Instance(1.0, );
        
        // - onlineStatus (Nominal)
        FastVector attValsRel = new FastVector();
        attValsRel.addElement("y");
        attValsRel.addElement("n");
        atts.addElement(new Attribute(atrributeNames[13], attValsRel));

        // - availability (Nominal)
        attValsRel = new FastVector();
        String availabilityString[] = new String[] {
            "completely orderable","completely not orderable",
            "mainly orderable","mixed","mainly not orderable",
            "completely not determinable","mainly not determinable"
        };
        for(int i=0; i < availabilityString.length; i++)
            attValsRel.addElement(availabilityString[i]);
        atts.addElement(new Attribute(atrributeNames[14], attValsRel));
        
        // - customerID,maxVal,customerScore,accountLifetime,
        // - payments,age,address,lastOrder (Numeric)
        for(int i=15; i < 23 ; i++) {
            atts.addElement(new Attribute(atrributeNames[i]));
        }

        // Order (Nominal)
        attValsRel = new FastVector();
        attValsRel.addElement("y");
        attValsRel.addElement("n");
        atts.addElement(new Attribute(atrributeNames[23], attValsRel));
        
        dataInstances = new Instances("DataModel", atts, 280000);
        dataInstances.setClassIndex(dataInstances.numAttributes()-1);
    }
    /**
     * Simple Load Model
     * @param path File Path Model
     * @return
     */
    public static Classifier LoadModel(String path) {
        try {
            return (Classifier) weka.core.SerializationHelper.read(path);
        } catch (Exception ex) {
            Logger.getLogger(GaIBReviewer.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }
    
    public double submitTransaction(String[] transaction) {
        // Make an evaluation
        
        double[] vals = new double[dataInstances.numAttributes()];
        
        // - sessionNo,startHour,startWeekday,duration,cCount,
        // - cMinPrice,cMaxPrice,cSumPrice,bCount,bMinPrice,bMaxPrice,bSumPrice,bStep (Numeric)
        for(int i=0; i < 13 ; i++) {
            if(!transaction[i].equals("?")) {
                vals[i] = Double.parseDouble(transaction[i]);
            } else {
                vals[i] = Instance.missingValue();
            }
        }
        
        // - onlineStatus (Nominal)
        if(!transaction[13].equals("?")) {
            vals[13] = dataInstances.attribute(13).indexOfValue(transaction[13]);
        } else {
                vals[13] = Instance.missingValue();
        }
        
        // - availability (Nominal)
        if(!transaction[14].equals("?")) {
            vals[14] = dataInstances.attribute(13).indexOfValue(transaction[13]);
        } else {
            vals[14] = Instance.missingValue();
        }
        
        // - customerID,maxVal,customerScore,accountLifetime,
        // - payments,age,address,lastOrder (Numeric)
        for(int i=15; i < 22 ; i++) {
            if(!transaction[i].equals("?")) {
                vals[i] = Double.parseDouble(transaction[i]);
            } else {
                vals[i] = Instance.missingValue();
            }
        }
        
        vals[23] = Instance.missingValue();
        
        Instance data = new Instance(1.0, vals);
        dataInstances.add(data);
        data.setDataset(dataInstances);
        dataInstances.add(data);
        
        double classifyDouble=1.0;
        try{
            classifyDouble = cls.classifyInstance(data);
            
        } catch (Exception ex) {
            System.out.println(ex.toString());
            Logger.getLogger(GaIBReviewer.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        data.setClassValue(classifyDouble);
        
        return  classifyDouble;
    }

    public void submitOutcome(String result) {
        //
    }

}
